
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <taglib/fileref.h>
#include <taglib/tag.h>
#include <mysql/mysql.h>

#define is_extension(extensionname) (strcasecmp (extension,extensionname)==0)

#define IDLE_US 10000

MYSQL db;

int folder_filter (const struct dirent *file)
{
    if (file->d_type == DT_DIR && strcmp(file->d_name,".")!=0 && strcmp (file->d_name, "..")!=0) return 1;
    else return 0;
}

int media_filter (const struct dirent *file, const char *extensions[], int nr_ext)
{
    int res = 0;
    char *extension = NULL;
    if (file->d_type == DT_REG)
    {
	extension = rindex (file->d_name, '.');
	if (extension!=NULL)
	{
	    extension++;
	}
	if (extension!=NULL)
	{
	    for (int i=0; i<nr_ext && res == 0; i++) 
		if (is_extension (extensions[i])) res=1;
	}
    }
    return res;
}

int media_music_filter(const struct dirent *file) 
{
    const char *ex[] = {"mpg", "mp2", "mp3", "wav", "wma", "ogg", "mid", "mod", "aac", "aiff"};
    return media_filter (file, ex, 10);
}

int media_video_filter(const struct dirent *file) 
{
    const char *ex[] = {"avi", "mpeg", "divx", "xvid", "mkv", "mp4", "flv", "mov", "rm", "rmv"};
    media_filter (file, ex, 10);
}

int find_media (const char *path)
{
    dirent **fileslist;
    int nr;
    // music
    if ((nr = scandir (path, &fileslist, media_music_filter, alphasort))>0)
    {
	for (int i=0; i < nr; i++)
	{
	    char temp[10000];
	    sprintf (temp, "%s/%s\0", path, fileslist[i]->d_name);
	    TagLib::FileRef mfile (temp);
	    printf ("%s - %s\n", mfile.tag()->artist().toCString(), mfile.tag()->title().toCString());
	    char qs[10000];
	    char artist[501];
	    mysql_escape_string (artist, mfile.tag()->artist().toCString(), strlen (mfile.tag()->artist().toCString()));
	    sprintf (qs, "INSERT INTO Artists (name) VALUES (\"%s\");", artist);
	    printf ("%s\n", qs);
	    mysql_query (&db, qs);
	    printf ("%s\n", mysql_error (&db));
	    usleep (IDLE_US);
	    free (fileslist[i]);
	}
	free (fileslist);
    }
    // video
    if ((nr = scandir (path, &fileslist, media_video_filter, alphasort))>0)
    {
	for (int i=0; i < nr; i++)
	{
	    printf ("%s/%s\n", path, fileslist[i]->d_name);
	    usleep (IDLE_US);
	    free (fileslist[i]);
	}
	free (fileslist);
    }
    // directories
    if ((nr = scandir (path, &fileslist, folder_filter, alphasort))>0)
    {
	for (int i=0; i < nr; i++)
	{
	    int size = strlen(path)+strlen(fileslist[i]->d_name)+2;
	    char *npath = (char*)malloc (size*sizeof(char));
	    if (npath!=NULL)
	    {
		snprintf (npath, size, "%s/%s\0", path, fileslist[i]->d_name);
		find_media (npath);
		free (npath);
	    }
	    // printf ("%s\n", fileslist[i]->d_name);
	    free (fileslist[i]);
	}
	free (fileslist);
    }
    return 0;
}

int main ()
{
    mysql_real_connect (&db, "localhost", "root", "", "music", 3306, NULL, 0);
    // printf ("%s\n", mysql_error (&db));
    find_media ("/mnt/Video");
    mysql_close (&db);
    return 0;
}
